package com.github.yoojia.mqtt.util;

import com.parkingwang.lang.kit.ByteKit;

/**
 * @author Yoojia Chen (yoojiachen@gmail.com)
 * @since 1.0
 */
final public class LimitedString {

    private LimitedString() {
    }

    public static String toString(int limited, byte[] bytes) {
        final int size = Math.min(limited, bytes.length);
        if (size <= 0) {
            return "";
        } else {
            return withEnd(new String(ByteKit.sliceBy(bytes, 0, size)), limited, bytes);
        }
    }

    private static String withEnd(String content, int limited, byte[] bytes) {
        return content + (bytes.length > limited ? " ..." : "");
    }
}
